// pages/register/register.js
// pages/register/register.js
const md5 = require('../../md5.js');
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    username_mail: "",
    password: "",
    nickname: "",
    confirm_password: "",
    username_mail_warn: "",
    nickname_warn: "",
    password_warn: "",
    confirm_password_warn: "",
    terminal_password: "",
    register_error: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  //注册用户名名称
  bindInputName: function (e) {
    let that = this;

    //let username_mail = e.detail.value.trim();
    let username_mail = e.trim();

    /*
    **var test = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(username_mail);
    **根据正则判断 是否是邮箱 符合邮箱格式 则test == true 不符合 test==false
    */
    if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(username_mail))) {
      //  wx.showToast({ title: '邮箱输入有误', duration: 2000, icon: 'none' });  

      that.setData({
        username_mail_warn: "邮箱格式有误"
      });

      return;
    }

    that.setData({
      username_mail: username_mail,
      username_mail_warn: ""
    });
  },

  //注册昵称
  bindInputnickName: function (e) {
    let that = this;
    //let nickname = e.detail.value.trim();
    let nickname = e;

    if (nickname === "") {
      that.setData({
        nickname_warn: "昵称不能为空"
      });

      return;
    }

    that.setData({
      nickname: nickname,
      nickname_warn: ""
    })

  },

  //注册密码
  bindInputPwd: function (e) {
    let that = this;
    //let password = e.detail.value.trim();
    let password = e;

    if (password === "") {
      that.setData({
        password_warn: "密码不能为空"
      });

      return;
    }

    that.setData({
      password: password,
      password_warn: ""
    });
  },

  //确认密码
  bindInputconfirmPwd: function (e1,e2) {
    let that = this;

    //let pwd = that.data.password;
    let pwd = e1;
    //let confirm_pwd = e.detail.value.trim();
    let confirm_pwd = e2;

    if (confirm_pwd === "") {
      that.setData({
        confirm_password_warn: "确认密码不能为空"
      });
      return;
    }

    if (pwd !== confirm_pwd) {
      that.setData({
        confirm_password_warn: "2次密码不一致"
      });
      return;
    }

    that.setData({
      confirm_password: confirm_pwd,
      terminal_password: confirm_pwd,
      confirm_password_warn: ""
    });
  },

  bindToRegister: function (e) {
    let that = this;
    if ((that.data.username_mail === "") || (that.data.terminal_password === "") || (that.data.nickname === "")) {
      that.setData({
        register_error: "注册信息不正确!"
      });
      return;
    }

    var header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      url: 'https://119.3.46.145/server/signin?name=' + that.data.username_mail + '&pwd=' + md5.hex_md5(that.data.terminal_password) + '&nick=' + that.data.nickname + '&appid=' + app.globalData.AppID + '&openid=' + app.globalData.openid,
      method: 'POST',
      header: header,
      success: res => {
        
        //console.log(res, "res ");

        //0表示注册成功,1表示注册的用户名已存在
        if(res.data.error === "0"){
          wx.showModal({
            title: '提示',
            content: '注册成功,返回登录页面',
          })
          wx.navigateTo({
            url: '../login/login',
          });
        }
        if(res.data.error === "1"){
          wx.showModal({
            title: '注册警告',
            content: '该用户名已注册，请找回或者重新注册新账号',
          })
        }

      },
      fail: function (error) {
        wx.showModal({
          title:'提示',
          content: JSON.stringify(error)
        });
      
      },
      complete: function (complete) {

      }
    })
  },

  formSubmit:function(e){
    let that = this;
    let input_nickname = e.detail.value.input_nickname;
    let input_password = e.detail.value.input_password;
    let input_password_confirm = e.detail.value.input_password_confirm;
    let input_username = e.detail.value.input_username;
    that.bindInputName(input_username);
    that.bindInputnickName(input_nickname);
    that.bindInputPwd(input_password);
    that.bindInputconfirmPwd(input_password, input_password_confirm);

    that.bindToRegister();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})